﻿#include <QApplication>
#include "cscreenprotectormag.h"
#include <QDebug>
#include "cdisplayinfo.h"
#include "cscreenprotectordlg.h"
#include "cscreenprotectorpreviewdlg.h"
#include "cruntimesetting.h"
#include "chook.h"

QCursor CScreenProtectorMag::m_OriginCusor;

void CScreenProtectorMag::Initialize()
{
    ss::CRuntimeSetting::Initialize();
    m_bHasShowScreenProtectDlg = false;
    m_dwAppStartTime           = GetTickCount();
    m_dwLastInputEventTime     = 0;
    m_bInPreviewMode           = false;
    m_OriginCusor              = m_pParent->cursor();
    m_timer                    = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));
    m_timer->start(TIMER_CHECK_SPAN);

    // install hook

    ss::CHook::Instance().InstallHook([this](void){
        this->m_dwLastInputEventTime = GetTickCount();
        this->OnAnyInput();
    });
 }

CScreenProtectorMag::CScreenProtectorMag(QObject *parent) : QObject(parent)
{
    m_pParent = dynamic_cast<QWidget*>(parent);
    Initialize();
}

void CScreenProtectorMag::Preview(bool bShowSecond, bool bShowDate, unsigned int nLockSpan, bool bRunOnStartup, const QString& sentence)
{
    m_bInPreviewMode = true;
    m_bHasShowScreenProtectDlg = true;
    CreatePreviewScreenProtecter();
}


void CScreenProtectorMag::CreateScreenProtecter()
{
    ss::CDisplayInfo display;
    display.ReadDisplayInfo();
    for (auto iter = display.GetDisplays().begin(); iter != display.GetDisplays().end(); ++iter)
    {
        CScreenProtectorDlg* dlg = new CScreenProtectorDlg(m_pParent);
        connect(this, SIGNAL(toDestroyScreenProtecter(int)), dlg, SLOT(done(int)));
        dlg->move(iter->geometry.left(), iter->geometry.top());
        dlg->resize(iter->size);
        dlg->show();
    }
    // hide mouse cursor
    QApplication::setOverrideCursor(Qt::BlankCursor);
}

void CScreenProtectorMag::CreatePreviewScreenProtecter()
{
    ss::CDisplayInfo display;
    display.ReadDisplayInfo();
    for (auto iter = display.GetDisplays().begin(); iter != display.GetDisplays().end(); ++iter)
    {
        CScreenProtectorPreviewDlg* dlg = new CScreenProtectorPreviewDlg(m_pParent);
        connect(this, SIGNAL(toDestroyScreenProtecter(int)), dlg, SLOT(done(int)));
        dlg->move(iter->geometry.left(), iter->geometry.top());
        dlg->resize(iter->size);
        dlg->show();
    }
    // hide mouse cursor
    QApplication::setOverrideCursor(Qt::BlankCursor);
}

void CScreenProtectorMag::OnAnyInput()
{
    // notify screen protect dlg destroy
    //qDebug() << __FUNCTION__ << ": threadID=" << GetCurrentThreadId();
    if (m_bHasShowScreenProtectDlg)
    {
        emit toDestroyScreenProtecter(0);
        m_bHasShowScreenProtectDlg = false;
        m_bInPreviewMode = false;
        QApplication::setOverrideCursor(m_OriginCusor);
    }
}

void CScreenProtectorMag::OnTimerOut()
{
    if (!m_bInPreviewMode && !m_bHasShowScreenProtectDlg &&
            ((GetTickCount() - max(m_dwLastInputEventTime, m_dwAppStartTime)) >= ss::CRuntimeSetting::GetConfig().nLockSpan))
    {
        m_bHasShowScreenProtectDlg = true;
        CreateScreenProtecter();
    }
}
